{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 738,
   "id": "d78910dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_dir = 'E:\\\\code\\\\notebookPrjs\\\\nowcoder-interview-process-master\\\\data'\n",
    "input_file = data_dir + '\\\\input.txt'\n",
    "input_bak_file = data_dir + '\\\\input_bak.txt'\n",
    "output_left_file = data_dir + '\\\\output_left.txt'\n",
    "output_match_file = data_dir + '\\\\output_match.txt'\n",
    "output_flt_file = data_dir + '\\\\output_filter.txt'\n",
    "not_fit_urls_file = data_dir + '\\\\not_fit_urls.txt'\n",
    "output_fit_file = data_dir + '\\\\output_fit.txt'\n",
    "key_words = ['滑动窗口', 'Syn', 'dns', '网页', '套接字', '连接池', '数据包', 'webserver', 'url', '半连接', '中间人', '帧头', '连接', '子网', '网址', '慢开始']\n",
    "use_re_match = False\n",
    "#use_re_match = True\n",
    "#update_input_file = False\n",
    "update_input_file = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 739,
   "id": "8cc96442",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "update input file\n"
     ]
    }
   ],
   "source": [
    "if update_input_file:\n",
    "    print('update input file')\n",
    "    # backup input file\n",
    "    with open(input_file, 'r', encoding='utf-8') as source_file:\n",
    "        content = source_file.read()\n",
    "    with open(input_bak_file, 'w', encoding='utf-8') as target_file:\n",
    "        target_file.write(content)\n",
    "    \n",
    "    # update input file    \n",
    "    with open(output_left_file, 'r', encoding='utf-8') as source_file:\n",
    "        content = source_file.read()\n",
    "    with open(input_file, 'w', encoding='utf-8') as target_file:\n",
    "        target_file.write(content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 740,
   "id": "518df481",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Record:\n",
    "    def __init__(self, text, link):\n",
    "        self.text_ = text\n",
    "        self.link_ = link\n",
    "        \n",
    "    def text(self):\n",
    "        return self.text_\n",
    "    \n",
    "    def link(self):\n",
    "        return self.link_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 741,
   "id": "950e3de8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total records count is 98\n"
     ]
    }
   ],
   "source": [
    "recs = []\n",
    "row_index = 0\n",
    "text = ''\n",
    "link = ''\n",
    "with open(input_file, 'r', encoding='utf-8') as file:\n",
    "    # 使用for循环逐行读取文件内容\n",
    "    for line in file:\n",
    "        # 打印每一行的内容。注意，这里每行末尾可能会有换行符'\\n'，\n",
    "        # 如果不希望在打印结果中保留换行符，可以使用rstrip()方法去除。\n",
    "        # print(row_index)\n",
    "        ri = row_index % 3\n",
    "        if ri == 0:\n",
    "            text = line.rstrip('\\n')\n",
    "        elif ri == 1:\n",
    "            if line.strip() != '链接':\n",
    "                print(row_index)\n",
    "                print(line)\n",
    "                break\n",
    "        if ri == 2:\n",
    "            link = line.rstrip('\\n')\n",
    "            rec = Record(text, link)\n",
    "            recs.append(rec)\n",
    "        #   print(len(recs))\n",
    "        # print(line.rstrip('\\n'))\n",
    "        row_index += 1\n",
    "\n",
    "print('Total records count is ' + str(len(recs)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 742,
   "id": "ba64dfb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# unfit_urls = []\n",
    "# with open(not_fit_urls_file, 'r', encoding='utf-8') as file:\n",
    "#     for line in file:\n",
    "#         unfit_urls.append(line.rstrip())\n",
    "# print('Total unfit urls count is ' + str(len(unfit_urls)))\n",
    "# print(unfit_urls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 743,
   "id": "c1c90993",
   "metadata": {},
   "outputs": [],
   "source": [
    "# fit_recs = []\n",
    "# for rec in recs[:]:\n",
    "#     link = rec.link().strip()\n",
    "#     #print(link)\n",
    "#     if link in unfit_urls:\n",
    "#         print('unfit url is ' + rec.link())\n",
    "#     else:\n",
    "#         fit_recs.append(rec)\n",
    "# print('Fit records count is ' + str(len(fit_recs)))\n",
    "# with open(output_fit_file, 'w', encoding='utf-8') as file:\n",
    "#     for rec in fit_recs:\n",
    "#         file.write(rec.text() + '\\n')\n",
    "#         file.write('\\t链接\\n')\n",
    "#         file.write(rec.link() + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 744,
   "id": "3014347e",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Left records count is 74\n",
      "Matched records count is 24\n",
      "6. 滑动窗口\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/397420930983624704?sourceSSR=search\n",
      "9. Syn泛洪攻击\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/397420930983624704?sourceSSR=search\n",
      "DNS原理\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353153920258220032?sourceSSR=search\n",
      "访问网页过程\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353157622037684224?sourceSSR=search\n",
      "套接字\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353156474530635776?sourceSSR=search\n",
      "某度短网址_牛客题霸_牛客网 (nowcoder.com)\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/390564913389875200?sourceSSR=search\n",
      "3.连接池，\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/feed/main/detail/78ed306ea01a466a87297a430853415c?sourceSSR=search\n",
      "数据包传输，给你起始位置和大小，只要有重叠就删除\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/feed/main/detail/ec091e8a7a774afa8afc1449f44edb22?sourceSSR=search\n",
      "3. webserver\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/feed/main/detail/a3b8f20dd6a44b77bef9b23e53ce22b8?sourceSSR=search\n",
      "2.输入URL的全过程，越详越好\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353156944850526208?sourceSSR=search\n",
      "dns什么）。\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353157108407410688?sourceSSR=search\n",
      "半连接\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/544916782528696320?sourceSSR=search\n",
      "如何防止中间人攻击\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/407195185350819840?sourceSSR=search\n",
      "滑动窗口原理\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353157761145970688?sourceSSR=search\n",
      "2 syn攻击   \n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/382287465963438080?sourceSSR=search\n",
      "套接字的过程 \n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353159364947156992?sourceSSR=search\n",
      "* 帧头内容\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/feed/main/detail/276937ddc5624d2583e6864e511b0b6d?sourceSSR=search\n",
      "长连接\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353157742179328000?sourceSSR=search\n",
      "15.数据包传输报文的一些细节\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/feed/main/detail/ec091e8a7a774afa8afc1449f44edb22?sourceSSR=search\n",
      "3.子网掩码\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353156944850526208?sourceSSR=search\n",
      "一个URL访问的全过程。\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/534839396101632000?sourceSSR=search\n",
      "6.连接池\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353158542871961600?sourceSSR=search\n",
      "6、输入网址后，一系列变化\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353156694618349568?sourceSSR=search\n",
      "慢开始怎么做计数器增加的\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353157673266913280?sourceSSR=search\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "match_recs = []\n",
    "left_recs = []\n",
    "for rec in recs[:]:  # 使用original_list的副本进行迭代，避免修改过程中迭代器失效\n",
    "    match = False\n",
    "    if use_re_match:\n",
    "        for key_word in key_words:\n",
    "            if re.match(key_word, rec.text()):\n",
    "                match = True\n",
    "                break\n",
    "            #new_list.append(num)\n",
    "            #original_list.remove(num)\n",
    "    else:\n",
    "        for key_word in key_words:\n",
    "            key_word_lower = key_word.lower()\n",
    "            if key_word_lower in rec.text().lower():\n",
    "                match = True\n",
    "                break\n",
    "    if match:\n",
    "        match_recs.append(rec)\n",
    "    else:\n",
    "        left_recs.append(rec)\n",
    "\n",
    "print('Left records count is ' + str(len(left_recs)))\n",
    "print('Matched records count is ' + str(len(match_recs)))\n",
    "for rec in match_recs:\n",
    "    print(rec.text())\n",
    "    print('\\t链接')\n",
    "    print(rec.link())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 745,
   "id": "3af1132b",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(output_match_file, 'w', encoding='utf-8') as file:\n",
    "    for rec in match_recs:\n",
    "        file.write(rec.text() + '\\n')\n",
    "        file.write('\\t链接\\n')\n",
    "        file.write(rec.link() + '\\n')\n",
    "        \n",
    "with open(output_left_file, 'w', encoding='utf-8') as file:\n",
    "    for rec in left_recs:\n",
    "        file.write(rec.text() + '\\n')\n",
    "        file.write('\\t链接\\n')\n",
    "        file.write(rec.link() + '\\n')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
